package stack;


/**
 * https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/
 *
 * https://mp.weixin.qq.com/s?__biz=MzAxODQxMDM0Mw==&mid=2247493266&idx=1&sn=6ff2a405706303e5e6f7f4a5d5984cb6&chksm=9bd4189aaca3918c79a6eaff9871b8dfbe29d822530ed53e7b22d1af13bbb590e7acfa3aa1bb&scene=178&cur_album_id=2165065740855443458#rd
 */
public class _921_使括号有效的最少添加 {

    public int minAddToMakeValid(String s) {
        int res = 0;
        int need = 0;

        for (int i = 0; i < s.length(); i++) {
            Character c = s.charAt(i);
            if (c.equals('(')) {
                need++;
            }

            if (c.equals(')')) {
                need--;
            }

            if (need == -1) {
                need = 0;
                res++;
            }
        }

        return res + need;
    }
}
